package com.minyifang.wcspf.work.ui.user.presenter;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.minyifang.wcspf.common.base.BasePresenter;
import com.minyifang.wcspf.common.config.network.Constants;
import com.minyifang.wcspf.common.config.network.RetrofitHelper;
import com.minyifang.wcspf.common.config.user.UserManager;
import com.minyifang.wcspf.common.md5.SafeUtils;
import com.minyifang.wcspf.work.model.HttpRespond;
import com.minyifang.wcspf.work.ui.goods.model.MyCommentBean;
import com.minyifang.wcspf.work.ui.user.view.MyCommentFView;

import org.json.JSONObject;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import io.reactivex.functions.Consumer;
import okhttp3.MediaType;
import okhttp3.RequestBody;

public class MyCommentFPresenter extends BasePresenter<MyCommentFView> {
    /**
     * 获取商品详情
     */
    public void getMyCommentList(int type, int page) {
        Map<String, Object> data = new HashMap<>();
        data.put("type", type);
        data.put("page", page);
        data.put("size", 10);
        final JSONObject jsonData = Constants.getAESJsonData(data, UserManager.getInstance().getToken());
        RequestBody requestBody = RequestBody.create(MediaType.parse("application/json"), jsonData.toString());
        addTask(RetrofitHelper.getInstance().getService().getMyCommentList(requestBody), new Consumer<HttpRespond<String>>() {
            @Override
            public void accept(HttpRespond<String> stringHttpRespond) throws Exception {
                if (stringHttpRespond.result == 1) {
                    String data = SafeUtils.decrypt(stringHttpRespond.data);
                    List<MyCommentBean> list = new Gson().fromJson(data, new TypeToken<List<MyCommentBean>>() {
                    }.getType());
                    getView().onGetMyCommentListSucceed(list);
                } else {
                    getView().onRequestFailed(stringHttpRespond.message);
                }
            }
        });
    }
}
